//
//  StatusCell.swift
//  Weibo
//
//  Created by apple on 15/12/12.
//  Copyright © 2015年 idcast. All rights reserved.
//

import UIKit
import SnapKit
///微博cell中控件的间距数值
let StatusCellMargin: CGFloat = 12
///微博头像的宽度
let StatusCellIconWidth: CGFloat = 35

class StatusCell: UITableViewCell {
    ///微博视图模型
    var viewModel: StatusViewModel? {
        didSet {
            //把视图模型赋值给顶部视图模型
            topView.viewModel = viewModel
            
            contentLabel.text = viewModel?.status.text
            
            //设置配图试图 设置试图模型之后 配图驶入有能力计算大小
            pictureView.viewModel = viewModel
            
            pictureView.snp_updateConstraints { (make) -> Void in
            make.height.equalTo(pictureView.bounds.height)
                //直接设置宽度数值
            make.width.equalTo(pictureView.bounds.width)

            }
        }
    }
    //MARK: 根据制定的视图模型计算行高
    func rowHeight(vm: StatusViewModel) -> CGFloat {
    
        //1 记录视图模型 会调用didset设置内容以及更新 约束
        viewModel = vm
        //2 强制更新约束 所有的控件fram都会被计算正确
        contentView.layoutIfNeeded()
        //3 返回底部视图的最大高度
        return CGRectGetMaxY(bottomView.frame)
        
    }
    //MARK: 构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    ///MARK: 懒加载控件 
    ///顶部视图
    private lazy var topView: StatusCellTopVIew = StatusCellTopVIew()
    ///微博正文标签
     lazy var contentLabel: UILabel = UILabel(title: "微博正文", fontSize: 14, color: UIColor.darkGrayColor(), screenInset: StatusCellMargin)
    ///配图信息
    lazy var pictureView: StatusPictureView = StatusPictureView()
    ///底部视图
     lazy var bottomView: StatusCellBottomVIew = StatusCellBottomVIew()
    
}
///MARK 设置界面
extension StatusCell {

     func setupUI() {
    
        //1 添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)

        //2 自动布局
        //顶部视图
       topView.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(contentView.snp_top)
        make.left.equalTo(contentView.snp_left)
        make.right.equalTo(contentView.snp_right)
        make.height.equalTo(2 * StatusCellMargin + StatusCellIconWidth)
        }
        //正文标签
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)

        }

        //底部视图
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
            
            //指定向下的约束
//            make.bottom.equalTo(contentView.snp_bottom)
        }
    }

}